package com.burgeon.backend.Rest.Template.Reviews;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.RetailData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 获取自提订单
 * */
public class FindRetailDataZT extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public FindRetailDataZT(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<RetailData> getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(), url);
        System.err.println(str);

        /*  String count = new JSONArray(str).getJSONObject(0).getString("count");*/
        ArrayList<RetailData> data = new ArrayList<RetailData>();

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        for (int i =0;i<jsonArray.length();i++){

            RetailData retailData = new RetailData();
            retailData.setId(jsonArray.getJSONArray(i).getString(0));
            retailData.setDate(jsonArray.getJSONArray(i).getString(1));
            retailData.setCode(jsonArray.getJSONArray(i).getString(2));
            retailData.setOpenid(jsonArray.getJSONArray(i).getString(3));
            retailData.setStore(jsonArray.getJSONArray(i).getString(4));
            retailData.setAddress(jsonArray.getJSONArray(i).getString(5));
            retailData.setGoodsName(jsonArray.getJSONArray(i).getString(6));
            retailData.setSinceDate(jsonArray.getJSONArray(i).getString(7));
            retailData.setXsno(jsonArray.getJSONArray(i).getString(8));
            retailData.setSelfRaiNo(jsonArray.getJSONArray(i).getString(9));
            data.add(retailData);
        }

        return data;
    }

    private MultiValueMap Data() {

        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("'YC_RETAIL_ID;ID'");
        columns.add("'YC_RETAIL_ID;BILLDATE'");//单据日期1
        columns.add("'YC_RETAIL_ID;DOCNO'");//单据编号2
        columns.add("'YC_RETAIL_ID;C_VIP_ID;OPENID'");

        columns.add("'C_STORE_ID;NAME'");//发货店仓名称
        columns.add("'C_STORE_ID;ADDRESS'");//发货店仓地址

        columns.add("'M_PRODUCT_ID;VALUE'");//商品名称

        columns.add("'YC_RETAIL_ID;SELFRAIS_DATE'");//自提日期

        columns.add("'YC_RETAIL_ID;XSNO'");//线上单号
        columns.add("'YC_RETAIL_ID;SELFRAISNO'");//自提码

        params.put("columns", columns);
        params.put("table", "YC_RETAILITEM");//表名

        params.put("count", "true");


        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();


        expr1.put("combine","and");
        expr1v1.put("column","'YC_RETAIL_ID;STATUS'");
        expr1v1.put("condition","2");
        expr1.put("expr1",expr1v1);
        expr1v2.put("column","'YC_RETAIL_ID;IS_SELFRAIS'");
        expr1v2.put("condition","Y");
        expr1.put("expr2",expr1v2);

        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = dateFormat.format(new Date());

        expr2.put("combine","and");
        expr2v1.put("column","'YC_RETAIL_ID;C_VIP_ID;OPENID'");
        expr2v1.put("condition","is not null");
        expr2.put("expr1",expr2v1);
        expr2v2.put("column","'YC_RETAIL_ID;SELFRAIS_DATE'");
        expr2v2.put("condition","'="+format+"'");
        expr2.put("expr2",expr2v2);

        paramss.put("combine","and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindRetailDataZT g = new FindRetailDataZT("39.104.240.134:55321", "nea@burgeon.com.cn", "root55321");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
